from src.game.entity.entity import Entity
from src.game.entity.player import EntityPlayer
from src.game.item import Item
from src.handler.sprite import SpriteHandler

class EntityItem(Entity):
    
    name = "item"
    
    def __init__(self):
        Entity.__init__(self)
        self.item = None
        self.collision = True
        self.influence = True
        self.width = 0.5
        self.height = 0.5
        
    def getSprite(self):
        return SpriteHandler.get("placeholder")
        
    def setItem(self, item):
        self.item = item
        
    def onCollision(self, entity):
        Entity.onCollision(self, entity)
        if isinstance(entity, EntityPlayer) and self.item is not None:
            entity.pickUpItem(self.item)
            
        
    def render(self, client, viewport):
        Entity.render(self, client, viewport)
        localX, localY = viewport.globalToLocale(self.x, self.y)
        sprite = self.getSprite()
        client.window.surface.blit(sprite, (localX * 64, localY * 64))
        
    def readFromPSO(self, pso):
        Entity.readFromPSO(self, pso)
        self.item = Item.loadFromPSO(pso)
        
    def writeToPSO(self, pso):
        Entity.writeToPSO(self, pso)
        if self.item is not None:
            self.item.writeToPSO(pso)